package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.MinIoTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.thread.WmUserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Autowired
    private MinIoTemplate minIoTemplate;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) throws IOException {
        //1.将前端传递的文件转为输入流
        InputStream in = multipartFile.getInputStream();
        //2.使用minio上传
        //获取文件后缀
        String contentType = multipartFile.getContentType();
        //判空
        if (StringUtils.isEmpty(contentType)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR);
        }
        String[] split = contentType.split("/");
        String s = split[1];

        String url = minIoTemplate.uploadFile("", System.currentTimeMillis() + "." + s, in, "image");

        //3.将图片地址保存到数据库
        //补齐参数
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setType((short) 0);
        wmMaterial.setUrl(url);
        //从threadlocal中获取userid
        WmUser wmUser = WmUserThreadLocal.get();
        wmMaterial.setUserId(wmUser.getId());

        int insert = wmMaterialMapper.insert(wmMaterial);
        if (insert < 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.MATERIASL_REFERENCE_FAIL);
        }


        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult list(WmMaterialDto dto) {
        //非空判断
        if (dto == null) {
            return new PageResponseResult(0,0,0);
        }
        Short isCollection = dto.getIsCollection();
        //构造page对象
        Integer page1 = dto.getPage();
        Integer size = dto.getSize();
        Page<WmMaterial> page = new Page<>(page1, size);
        //设置查询条件
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(isCollection == 1, WmMaterial::getIsCollection, isCollection);
        Page<WmMaterial> resultPage = wmMaterialMapper.selectPage(page, wrapper);

        Long total = resultPage.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(page1,size,total.intValue());
        pageResponseResult.setData(resultPage.getRecords());

        return pageResponseResult;

    }
}
